$(function () {
  $('.news-more').addClass('new-more-hide');
  var cur = 1;
  var total = 0;
  if(window.screen.width<750){
    curPage = 1
    getData(curPage);
    $('#footer').hide()
    
    //滚动事件
    $(window).on('scroll', function() {
      var scrollTop = $(document).scrollTop();
      var scrollHeight = $(document).height() - $(window).height();
      if (scrollTop >= scrollHeight && $('.m-news-tabs .news-font').eq(1).hasClass("active")) {
        if($('.news-more').hasClass('new-more-hide')){
          curPage++
          getData(curPage);
        }
      }
    });
    return
  }
  
  // 页码点击事件
  $('#my-pagination').find('.ui-page-number').click(
    function () {
      index = $(this).index();
      pageFn(index)
    }
  )
  init(cur)
});
//点击函数
function getMore(){
  curPage++
  getData(curPage)
}
//移动端新闻获取
function getData(cur){
  $.ajax({
    dataType: 'jsonp',
    url: 'https://wapp.mofisher.com/v2/office/journalism?size=5&page=' + cur,
    success: function (data) {
      if (data.code != '2000') {
        return;
      }
      var resData = data.datainfo.news;
      if(resData.length==0 && window.screen.width<750){
        $('.news-more').removeClass('new-more-hide').addClass('new-more-show');
        $('#footer').show()
      }
      for (var i = 0; i < resData.length; i++) {
        var str = '<li class="news-item"><div class="news-item-slt"><img src="' + resData[i].img_url + '" alt="加载失败"></div><div class="right-content"><div class="right-title"><a href="news-detail.html?id=' + resData[i].id + '">' + resData[i].title +
          '</a></div><div class="middle-content"><span class="source-name">文章来源:</span><span class="source">' + resData[i].type + '</span><span>' + resData[i].time_one + '</span></div>' +
          '<div class="right-detail">' + resData[i].generalize + '<span class="btn"><a href="news-detail.html?id=' + resData[i].id + '">查看全文 &gt;</a></span></div><div class="right-all"><span>' + resData[i].time_two + '</span><span>/新闻/</span>' +
          '<span>By yunzao</span></div></div></li>';
        $('.J_newList').append(str);
      }
    }
  })
}

function init(cur) {
  $.ajax({
    dataType: 'jsonp',
    url: 'https://wapp.mofisher.com/v2/office/journalism?size=5&page=' + cur,
    success: function (data) {
      if (data.code != '2000') {
        return;
      }
      //处理data数据
      var resData = data.datainfo.news;
      var paginalNumber = data.datainfo.pageCount;
      $('.J_newList').html('');
      $('#my-pagination').html("<span class='ui-page-pre' " + 'onClick="init(' + (cur - 1) + ')">' + "<span class='tri'></span></span><span class='ui-page-box'></span><span class='ui-page-next'" + 'onClick="init(' + (cur + 1) + ')">' + "<span class='next-tri'></span></span>");
      
      //首页、尾页时前进后退按钮不能点击
      if(paginalNumber==1){
        $('#my-pagination').html("<span class='pre-disabel'>" + "<span class='tri'></span></span><span class='ui-page-box'></span><span class='pre-disabel'>" + "<span class='next-tri'></span></span>");
      }
      if ((cur == 1)&&(paginalNumber>1)) {
        $('#my-pagination').html("<span class='pre-disabel'>" + "<span class='tri'></span></span><span class='ui-page-box'></span><span class='ui-page-next'" + 'onClick="init(' + (cur + 1) + ')">' + "<span class='next-tri'></span></span>");
      }
      if ((cur == paginalNumber)&&(paginalNumber>1)) {
        $('#my-pagination').html("<span class='ui-page-pre' " + 'onClick="init(' + (cur - 1) + ')">' + "<span class='tri'></span></span><span class='ui-page-box'></span><span class='pre-disabel'>" + "<span class='next-tri'></span></span>");
      }
     
      for (var i = 0; i < resData.length; i++) {
        var str = '<li class="news-item"><div class="news-item-slt"><img src="' + resData[i].img_url + '" alt="加载失败"></div><div class="right-content"><div class="right-title"><a href="news-detail.html?id=' + resData[i].id + '">' + resData[i].title +
          '</a></div><div class="middle-content"><span class="source-name">文章来源:</span><span class="source">' + resData[i].type + '</span><span>' + resData[i].time_one + '</span></div>' +
          '<div class="right-detail">' + resData[i].generalize + '<span class="btn"><a href="news-detail.html?id=' + resData[i].id + '">查看全文 &gt;</a></span></div><div class="right-all"><span>' + resData[i].time_two + '</span><span>/新闻/</span>' +
          '<span>By yunzao</span></div></div></li>';
        $('.J_newList').append(str);
      }
      //中间部分
      var middle = '<span class="ui-page-number" data-number=' + (cur - 1) + ' onClick="init(' + (cur - 1) + ')">' + (cur - 1) + '</span>' + '<span class="ui-page-number" data-number=' + cur + ' onClick="init(' + cur + ')">' + cur + '</span>' + '<span class="ui-page-number" data-number=' + (cur + 1) + ' onClick="init(' + (cur + 1) + ')">' + (cur + 1) + '</span>'
      //省略号
      var text = '<span class="ui-page-number">...</span>'
      var frontNode = '<span class="ui-page-number" data-number=' + 1 + ' onClick="init(' + 1 + ')">' + 1 + '</span>' + '<span class="ui-page-number" data-number=' + 2 + ' onClick="init(' + 2 + ')">' + 2 + '</span>'
      //后面
      var backNode = '<span class="ui-page-number" data-number=' + (paginalNumber - 1) + ' onClick="init(' + (paginalNumber - 1) + ')">' + (paginalNumber - 1) + '</span>' + '<span class="ui-page-number" data-number=' + (paginalNumber) + ' onClick="init(' + (paginalNumber) + ')">' + (paginalNumber) + '</span>'

      //当前页小于3，页面总数大于8
      if ((cur <= 3) && (paginalNumber > 8)) {
        var innner = "";
        var innerFront = "";
        for (var j = 0; j < 4; j++) {
          innner += '<span class="ui-page-number" data-number=' + (j + 1) + ' onClick="init(' + (j + 1) + ')">' + (j + 1) + '</span>'
        }
        for (var z = paginalNumber - 4; z < paginalNumber; z++) {
          innerFront += '<span class="ui-page-number" data-number=' + (z + 1) + ' onClick="init(' + (z + 1) + ')">' + (z + 1) + '</span>'
        }
        $('#my-pagination').find('.ui-page-box').append(innner + text + innerFront);
        $('#my-pagination').find('.ui-page-number').eq(cur - 1).addClass('ui-page-act');
        return
      }

      //如果当前页里第一页大于2，当前页里最后一页大于2
      if ((paginalNumber > 8) && (cur > 3) && (paginalNumber - cur > 3)) {
        $('#my-pagination').find('.ui-page-box').append(frontNode + text + middle + text + backNode)
        $('#my-pagination').find('.ui-page-number').eq(4).addClass('ui-page-act');
        return
      }
      //当前页离最后一页3个长度
      if ((paginalNumber - cur <= 3) && (paginalNumber > 8)) {
        var inner3 = ""
        var inner3Front = ""
        for (var z = paginalNumber - 4; z < paginalNumber; z++) {
          inner3 += '<span class="ui-page-number" data-number=' + (z + 1) + ' onClick="init(' + (z + 1) + ')">' + (z + 1) + '</span>'
        }
        for (var ii = 0; ii < 4; ii++) {
          inner3Front += '<span class="ui-page-number" data-number=' + (ii + 1) + ' onClick="init(' + (ii + 1) + ')">' + (ii + 1) + '</span>'
        }
        $('#my-pagination').find('.ui-page-box').append(inner3Front + text + inner3)
        $('#my-pagination').find('.ui-page-number').eq(cur - 10).addClass('ui-page-act');
        return
      }
      var inner4 = "";
      for (var zz = 0; zz < paginalNumber; zz++) {
        inner4 += '<span class="ui-page-number" data-number=' + (zz + 1) + ' onClick="init(' + (zz + 1) + ')">' + (zz + 1) + '</span>'
      }
      $('#my-pagination').find('.ui-page-box').append(inner4)
      $('#my-pagination').find('.ui-page-number').eq(cur - 1).addClass('ui-page-act');

    }
  })
}
